package com.gopay.bis.tbank.expay.impl;

import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.tbank.expay.operation.ExPayBuildMessage;
import com.gopay.bis.tbank.expay.operation.ExPayParserMessage;
import com.gopay.common.constants.bank.ExtPayBankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ExPayBaseMessage;
import com.gopay.common.domain.bank.tbank.expay.TBankMessageExPay;
import com.gopay.common.domain.bank.tbank.expay.TBankMessageExPayAgreement;
import com.gopay.common.domain.bank.tbank.expay.TBankMessageExPayOrderment;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.businesses.ExPayService;

/**
 * 快捷支付测试银行
 * 
 * @TBankExPayServiceImpl.java
 * @author WangHongFei
 */
public class TBankExPayServiceImpl implements ExPayService {

    private HttpTransport httpTransport;
    private ExPayBuildMessage exPayBuildMessage;
    private ExPayParserMessage exPayParserMessage;

    public Object query(Object object) throws GopayException{
        TBankMessageExPay tBankMessageExPay = null;
        try {
            if (object instanceof TBankMessageExPayAgreement) {// 合约
                TBankMessageExPayAgreement tbankExPayAgree = (TBankMessageExPayAgreement) object;
                tBankMessageExPay = dealAgreement(tbankExPayAgree);
            } else if (object instanceof TBankMessageExPayOrderment) {// 支付
                TBankMessageExPayOrderment tBankExpayOrder = (TBankMessageExPayOrderment) object;
                tBankMessageExPay = dealOrderment(tBankExpayOrder);
            }
            return tBankMessageExPay;
        } catch (Exception e) {
        	BankLogUtils.printExPayExceptionLog(tBankMessageExPay, e);
        	throw new GopayException(ProcCodeConstants.PROC_CODE_100E6058);
        }         
    }

    private TBankMessageExPay dealOrderment(TBankMessageExPayOrderment tBankExpayOrder) throws Exception{
        Map tempMap = null;
        if (ExtPayBankConstants.EXTPAY_PAYORDER.equals(tBankExpayOrder.getExPayOperType())) {
            tempMap = this.exPayBuildMessage.buildExpayOrder(tBankExpayOrder);
        } else {
            tempMap = this.exPayBuildMessage.buildExpayOrderQuery(tBankExpayOrder);
        }
        this.httpTransport.setUrl(tBankExpayOrder.getBankUrl());
        //BankLogUtils.printExPayQueryReqLog(tBankExpayOrder, tempMap);
        Object tempObj = this.httpTransport.submit(tempMap);
        TBankMessageExPay tBankMessageExPay = null;
        if (ExtPayBankConstants.EXTPAY_PAYORDER.equals(tBankExpayOrder.getExPayOperType())) {
            tBankMessageExPay = this.exPayParserMessage.parseExpayOrder(tempObj);
        } else {
            tBankMessageExPay = this.exPayParserMessage.parseExpayOrderQuery(tempObj);
        }
        BankLogUtils.printExPayQueryResLog(tBankExpayOrder, tBankMessageExPay);
        return tBankMessageExPay;
    }

    private TBankMessageExPay dealAgreement(TBankMessageExPayAgreement tbankExPayAgree) throws Exception{
        TBankMessageExPay tBankMessageExPay = null;
        Map tempMap = this.exPayBuildMessage.buildExpayAgree(tbankExPayAgree);
        this.httpTransport.setUrl(tbankExPayAgree.getBankUrl());
        //BankLogUtils.printExPayQueryReqLog(tbankExPayAgree, tempMap);
        Object tempObj = this.httpTransport.submit(tempMap);
        if (ExtPayBankConstants.EXTPAY_CANCELAGREEMENT.equals(tbankExPayAgree.getExPayOperType())) {// 解约
            tBankMessageExPay = this.exPayParserMessage.parseExpayAgree(tempObj);
        } else {
            tBankMessageExPay = new TBankMessageExPay();
        }
        BankLogUtils.printExPayQueryResLog(tbankExPayAgree, tBankMessageExPay);
        return tBankMessageExPay;
    }

    public void setHttpTransport(HttpTransport httpTransport) {
        this.httpTransport = httpTransport;
    }

    public void setExPayBuildMessage(ExPayBuildMessage exPayBuildMessage) {
        this.exPayBuildMessage = exPayBuildMessage;
    }

    public void setExPayParserMessage(ExPayParserMessage exPayParserMessage) {
        this.exPayParserMessage = exPayParserMessage;
    }

	@Override
	public Object certification(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object sendSms(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object checkSms(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object sign(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object verify(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object treatyQuery(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object cancel(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object payment(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object queryOrder(BaseMessage baseMsg) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Object queryCvn(BaseMessage baseMsg) throws GopayException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object signQuery(BaseMessage baseMsg) throws GopayException {
        // TODO Auto-generated method stub
        return null;
    }

}
